package com.lgmshare.component.storage;

import android.content.Context;
import android.util.LruCache;

import com.lgmshare.component.annotation.Hide;

/**
 * 存储管理
 * 管理缓存数据，实现各种存储方式
 * <p>
 * #Properties缓存 -- 实现
 * #SharedPreferences -- 实现
 * #Database -- 实现
 * #File文件
 * </p>
 *
 * @author lim
 * @description: 默认使用缓存到内存+preference文件中，需要用其他缓存，需要直接指定存放方式
 * @email lgmshare@gmail.com
 * @datetime 2017/9/28 13:54
 */
@Hide
public final class DataKeeper {

    public static final String CACHE_PREFERENCE = "cache_preference";
    public static final String CACHE_PROPERTIES = "cache_properties";

    private static Context mContext;
    private static LruCache<String, Object> mLruCache;

    private DatabaseHelper mDatabaseHelper;
    private FileHelper mFileHelper;
    private PropertiesHelper mPropertiesHelper;
    private SharedPreferencesHelper mSharedPreferencesHelper;

    public static void init(Context context, int maxSize) {
        mContext = context.getApplicationContext();
        mLruCache = new LruCache<>(maxSize);
    }

    private final static class HolderClass {
        private final static DataKeeper INSTANCE = new DataKeeper();
    }

    public static DataKeeper getInstance() {
        return HolderClass.INSTANCE;
    }

    private DataKeeper() {
        mFileHelper = FileHelper.getInstance(mContext);
        mPropertiesHelper = PropertiesHelper.getInstance(mContext, CACHE_PROPERTIES);
        mDatabaseHelper = DatabaseHelper.getInstance(mContext);
        mSharedPreferencesHelper = SharedPreferencesHelper.getInstance(mContext, CACHE_PREFERENCE);
    }

    /**
     * 将数据保存到SharedPreferences
     *
     * @param key
     * @param value
     */
    public void put(String key, String value) {
        putToCache(key, value);
        mSharedPreferencesHelper.putString(key, value);
    }

    /**
     * 将数据保存到SharedPreferences
     *
     * @param key
     * @param value
     */
    public void put(String key, boolean value) {
        putToCache(key, value);
        mSharedPreferencesHelper.putBoolean(key, value);
    }

    /**
     * 将数据保存到SharedPreferences
     *
     * @param key
     * @param value
     */
    public void put(String key, int value) {
        putToCache(key, value);
        mSharedPreferencesHelper.putInt(key, value);
    }

    /**
     * 将数据保存到SharedPreferences
     *
     * @param key
     * @param value
     */
    public void put(String key, float value) {
        putToCache(key, value);
        mSharedPreferencesHelper.putFloat(key, value);
    }

    /**
     * 将数据保存到SharedPreferences
     *
     * @param key
     * @param value
     */
    public void put(String key, long value) {
        putToCache(key, value);
        mSharedPreferencesHelper.putLong(key, value);
    }

    /**
     * 将数据保存到缓存中
     *
     * @param key
     * @param value
     */
    public void putToCache(String key, Object value) {
        if (null == value) {
            return;
        }
        if (null == mLruCache) {
            throw new NullPointerException("DataKeeper Not Initialized");
        }
        mLruCache.put(key, value);
    }

    /**
     * 数据库存储只支持字符串
     *
     * @param key
     * @return
     */
    public void putToDB(String key, String value) {
        putToCache(key, value);
        mDatabaseHelper.insert(key, value);
    }

    /**
     * 将数据properties文件中
     *
     * @param key
     * @param value
     */
    public void putToProperty(String key, String value) {
        putToCache(key, value);
        mPropertiesHelper.setProperty(key, value);
    }

    /**
     * 获取key-value
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public String getString(String key, String defaultValue) {
        Object v = getFromCache(key);
        if (null == v) {
            String f = mSharedPreferencesHelper.getString(key, defaultValue);
            putToCache(key, f);
            return f;
        }
        if (v instanceof String) {
            return (String) v;
        }
        return defaultValue;
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        Object v = getFromCache(key);
        if (null == v) {
            Boolean f = mSharedPreferencesHelper.getBoolean(key, defaultValue);
            putToCache(key, f);
            return f;
        }
        if (v instanceof Boolean) {
            return (Boolean) v;
        }
        return defaultValue;
    }

    public int getInt(String key, int defaultValue) {
        Object v = getFromCache(key);
        if (null == v) {
            Integer f = mSharedPreferencesHelper.getInt(key, defaultValue);
            putToCache(key, f);
            return f;
        }
        if (v instanceof Integer) {
            return (Integer) v;
        }
        return defaultValue;
    }

    public float getFloat(String key, float defaultValue) {
        Object v = getFromCache(key);
        if (null == v) {
            Float f = mSharedPreferencesHelper.getFloat(key, defaultValue);
            putToCache(key, f);
            return f;
        }
        if (v instanceof Float) {
            return (Float) v;
        }
        return defaultValue;
    }

    public long getLong(String key, long defaultValue) {
        Object v = getFromCache(key);
        if (null == v) {
            Long f = mSharedPreferencesHelper.getLong(key, defaultValue);
            putToCache(key, f);
            return f;
        }
        if (v instanceof Long) {
            return (Long) v;
        }
        return defaultValue;
    }

    /**
     * 从缓存中获取key-value
     *
     * @param key
     * @return
     */
    private Object getFromCache(String key) {
        if (null == mLruCache) {
            return null;
        }
        return mLruCache.get(key);
    }

    /**
     * 数据库存储只支持字符串
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public String getFromDB(String key, String defaultValue) {
        Object v = getFromCache(key);
        if (null == v) {
            String f = mDatabaseHelper.query(key);
            if (null != f) {
                putToCache(key, f);
                return f;
            }
        }
        if (v instanceof String) {
            return (String) v;
        }
        return defaultValue;
    }

    public String getProperty(String key, String defaultValue) {
        Object v = getFromCache(key);
        if (null == v) {
            String f = mPropertiesHelper.getProperty(key, defaultValue);
            if (null != f) {
                putToCache(key, f);
                return f;
            }
        }
        if (v instanceof String) {
            return (String) v;
        }
        return defaultValue;
    }

    public void remove(String key) {
        mLruCache.remove(key);
        mSharedPreferencesHelper.remove(key);
    }

    public void removeToCache() {
        mLruCache.evictAll();
    }

    public void removeToDB(String key) {
        mLruCache.remove(key);
        mDatabaseHelper.delete(key);
    }

    public void removeToProperty(String key) {
        mLruCache.remove(key);
        mPropertiesHelper.removeProperty(key);
    }

    /**
     * 清除所有缓存数据
     */
    public void clear() {
        removeToCache();
        mDatabaseHelper.clear();
        mSharedPreferencesHelper.clear();
        mPropertiesHelper.clear();
    }
}
